//
//  HSSearchBar.m
//  Wutuojia
//
//  Created by Work on 15/7/19.
//  Copyright (c) 2015年 Work. All rights reserved.
//

#import "HSSearchBar.h"
//#import "ALUtilsMacro.h"
#import "ALUtils.h"

@implementation HSSearchBar
-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        UIImage* searchBarBg = [HSSearchBar GetImageWithColor:[UIColor clearColor] andHeight:32.0f];
        //设置背景图片
        [self setBackgroundImage:searchBarBg];
        //设置背景色
        [self setBackgroundColor:[UIColor clearColor]];
        //设置文本框背景
        [self setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self _actionCom:self];
}
-(void)_actionCom:(UISearchBar *)theSearchBar{
    
    float version = [[[UIDevice currentDevice] systemVersion ] floatValue ];
    if ([self respondsToSelector : @selector (barTintColor)]) {
        float  iosversion7_1 = 7.1 ;
        if (version >= iosversion7_1)
        {
            //iOS7.1
//            [[[[self.subviews objectAtIndex:0] subviews] objectAtIndex : 0 ] removeFromSuperview ];
//            [self setBackgroundColor:[UIColor clearColor]];
//            self.showsCancelButton=YES;
        }
        else
        {
            //iOS7.0
            [self setBarTintColor:[UIColor blackColor]];
            [self setBackgroundColor:[UIColor clearColor]];
        }
        
        // 经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象
//        self.barTintColor = [UIColor whiteColor];
        UITextField * searchTextField = [[[self.subviews firstObject] subviews] lastObject];
        [searchTextField.layer setBorderWidth:.5f];
        [searchTextField.layer setCornerRadius:2.0f];
        [searchTextField.layer setBorderColor:[UIColor clearColor].CGColor];
		[searchTextField setBackgroundColor:AL_RGB(239,242,245)];
        [searchTextField setFont:[UIFont systemFontOfSize:28/2]];
        [searchTextField setTextColor:AL_RGB(74,86,105)];
        
    }
    else
    {
        //iOS7.0 以下
        [[ self . subviews objectAtIndex : 0 ] removeFromSuperview ];
        [ self setBackgroundColor :[ UIColor clearColor ]];
        
        // iOS6以下版本searchBar内部子视图的结构不一样
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
                UITextField * searchTextField = (UITextField *)subView;
                [searchTextField.layer setBorderWidth:.5f];
                [searchTextField.layer setCornerRadius:2.0f];
                [searchTextField.layer setBorderColor:[UIColor blackColor].CGColor];
            }
        }
    }
}
/**
 *  生成图片
 *
 *  @param color  图片颜色
 *  @param height 图片高度
 *
 *  @return 生成的图片
 */
+ (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}
@end
